export default {
	// 为当前模块开启命名空间
	namespaced: true,

	// 模块的 state 数据
	state: {
		token: uni.getStorageSync('token') || '',
		userinfo: JSON.parse(uni.getStorageSync('userinfo') || '{}'), //用户信息
		accountNumber: JSON.parse(uni.getStorageSync('accountNumber') || '[]') //账号
	},

	// 模块的 mutations 方法
	mutations: {
		//添加账号
		addAccountNumber(state, accountNumber) {
			state.accountNumber.push(accountNumber)
			uni.setStorageSync('accountNumber', JSON.stringify(state.accountNumber))
		},
		//删除账号
		deleteAccountNumber(state, username) {
			state.accountNumber = state.accountNumber.filter(item => {
				return item.username != username
			})
			uni.setStorageSync('accountNumber', JSON.stringify(state.accountNumber))
		},
		// 更新密码
		updatePassword(state, newPwd) {
			state.accountNumber.filter(item => {
				return item.username == newPwd.username
			}).forEach(item => {
				item.password = newPwd.newPassword
			})
			uni.setStorageSync('accountNumber', JSON.stringify(state.accountNumber))
		},
		// 更新用户的基本信息
		updateUserInfo(state, userinfo) {
			state.userinfo = userinfo
			this.commit('login/saveUserInfoToStorage')
		},
		//修改手机号
		updatePhone(state, phone) {
			state.userinfo.phone = phone
			this.commit('login/saveUserInfoToStorage')
		},
		// 将 userinfo 持久化存储到本地
		saveUserInfoToStorage(state) {
			uni.setStorageSync('userinfo', JSON.stringify(state.userinfo))
		},

		updateToken(state, token) {
			state.token = token
			this.commit('login/saveTokenToStorage')
		},

		// 将 token 字符串持久化存储到本地
		saveTokenToStorage(state) {
			uni.setStorageSync('token', state.token)
		},
		deleteTokenToStorage(state) {
			uni.removeStorageSync('token');
			uni.removeStorageSync('userinfo');
			state.token = ''
			state.userinfo = {}
		},
	},

	// 模块的 getters 属性
	getters: {},
}